package com.restaurant.order;
import java.util.ArrayList;
import java.util.List;
class OrderItem {
    private Dish dish;
    private int quantity;

    public OrderItem(Dish dish, int quantity) {
        this.dish = dish;
        this.quantity = quantity;
    }

    public Dish getDish() { return dish; }
    public int getQuantity() { return quantity; }
}
class Order {
    private String orderId;
    private Customer customer;
    private List<OrderItem> itemList;
    private double totalAmount;

    public Order(String orderId, Customer customer) {
        this.orderId = orderId;
        this.customer = customer;
        this.itemList = new ArrayList<>();
        this.totalAmount = 0.0;
    }
    public void addDish(Dish dish, int quantity) throws Exception {
        if (!dish.isOnSale()) {
            throw new Exception("菜品【" + dish.getName() + "】已下架，无法购买");
        }
        if (dish.getStock() < quantity) {
            throw new Exception("菜品【" + dish.getName() + "】库存不足，当前库存：" + dish.getStock());
        }
        for (OrderItem item : itemList) {
            if (item.getDish().equals(dish)) {
                itemList.remove(item);
                quantity += item.getQuantity();
                break;
            }
        }
        dish.reduceStock(quantity);
        itemList.add(new OrderItem(dish, quantity));
        calculateTotal();
    }

    public void calculateTotal() {
        this.totalAmount = 0.0;
        for (OrderItem item : itemList) {
            this.totalAmount += item.getDish().getPrice() * item.getQuantity();
        }
    }

    public void showOrder() {
        System.out.println("===== 订单ID：" + orderId + " =====");
        System.out.println("顾客信息：" + customer.getInfo());
        System.out.println("菜品列表：");
        for (OrderItem item : itemList) {
            System.out.println("- " + item.getDish().getName()
                    + " × " + item.getQuantity()
                    + "，单价：" + item.getDish().getPrice()
                    + "，小计：" + (item.getDish().getPrice() * item.getQuantity()));
        }
        System.out.println("订单总价：" + totalAmount);
        System.out.println("======================");
    }
}